class Solution
{
public:
    long long makeIntegerBeautiful(long long n, int target)
    {
        long long mask = 1;
        long long result = 0;
        int digitSum = getDigitSum(n);
        while (digitSum > target)
        {
            int digit = getDigit(n, mask);
            if (digit != 0)
            {
                result += (10 - digit) * mask;
                n += (10 - digit) * mask;
            }
            digitSum = getDigitSum(n);
            mask *= 10;
        }
        return result;
    }

    inline int getDigit(long long n, long long mask)
    {
        return (n % (mask * 10)) / mask;
    }

    inline int getDigitSum(long long n)
    {
        long long mask = 1;
        int sum = 0;
        while (mask <= n)
        {
            sum += getDigit(n, mask);
            mask *= 10;
        }
        return sum;
    }
};